import java.util.Arrays;

public class Solution977 {
    public int[] sortedSquares(int[] nums) {
        int t=0;
        while (t<nums.length && nums[t]<0){
            t++;
        }
        int p=t-1;
        int c=0;
        int[] ans=new int[nums.length];
        while (p>=0 && t<nums.length){
            if (Math.abs(nums[p])<Math.abs(nums[t])){
                ans[c]=nums[p]*nums[p];
                p--;
            }else{
                ans[c]=nums[t]*nums[t];
                t++;
            }
            c++;
        }
        while (p>=0){
            ans[c]=nums[p]*nums[p];
            p--;
            c++;
        }
        while (t<nums.length){
            ans[c]=nums[t]*nums[t];
            t++;
            c++;
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution977().sortedSquares(new int[]{-4,-1,0,3,10})));
    }
}
